package net.happy2v.louis.re.tabmenu.common;

import net.happy2v.louis.re.R;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;

public class TabBaseListActivity extends TabActivity {

	public final static String INTENT_EXTRA_TAB_TYPE = "extra_tab_type";

	public final static int INTENT_EXTRA_TAB_TYPE_WORD = 0x101;
	public final static int INTENT_EXTRA_TAB_TYPE_SENTENCE = 0x102;

	public final int INDICATOR_ENGLISH = 1;
	public final int INDICATOR_KOREAN = 2;
	public final int INDICATOR_BOTH = 3;

	private final String TAG_TAB_ENGLISH = "tab_english";
	private final String TAG_TAB_KOREAN = "tab_korean";
	private final String TAG_TAB_BOTH = "tab_both";

	private int mType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_layout_tab1);

		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}

		if (extras.containsKey(INTENT_EXTRA_TAB_TYPE)) {
			mType = extras.getInt(INTENT_EXTRA_TAB_TYPE);
		}

		final TabHost tabHost = getTabHost();
		final TabWidget tabWidget = getTabWidget();
		tabWidget.setGravity(Gravity.BOTTOM);

		tabHost.addTab(tabHost
				.newTabSpec(TAG_TAB_ENGLISH)
				.setIndicator(buildIndicator(INDICATOR_ENGLISH))
				.setContent(
						new Intent(this, ListBaseActivity.class)
								.putExtra(INTENT_EXTRA_TAB_TYPE, mType)
								.putExtra(
										ListBaseActivity.INTENT_EXTRA_LANGUGE_TYPE,
										ListBaseActivity.INTENT_EXTRA_LANGUGE_TYPE_KOREAN)));
		tabHost.addTab(tabHost
				.newTabSpec(TAG_TAB_KOREAN)
				.setIndicator(buildIndicator(INDICATOR_KOREAN))
				.setContent(
						new Intent(this, ListBaseActivity.class)
								.putExtra(INTENT_EXTRA_TAB_TYPE, mType)
								.putExtra(
										ListBaseActivity.INTENT_EXTRA_LANGUGE_TYPE,
										ListBaseActivity.INTENT_EXTRA_LANGUGE_TYPE_ENGLISH)));
		tabHost.addTab(tabHost
				.newTabSpec(TAG_TAB_BOTH)
				.setIndicator(buildIndicator(INDICATOR_BOTH))
				.setContent(
						new Intent(this, ListBaseActivity.class)
								.putExtra(INTENT_EXTRA_TAB_TYPE, mType)
								.putExtra(
										ListBaseActivity.INTENT_EXTRA_LANGUGE_TYPE,
										ListBaseActivity.INTENT_EXTRA_LANGUGE_TYPE_BOTH)));
	}

	private View buildIndicator(int tabKind) {
		final RelativeLayout indicator = (RelativeLayout) getLayoutInflater()
				.inflate(R.layout.tab_indicator_btn_item, getTabWidget(), false);
		Button btn = (Button) indicator.findViewById(R.id.tab_menu_label);
		switch (tabKind) {
		case INDICATOR_ENGLISH:
			btn.setText("English");
			break;
		case INDICATOR_KOREAN:
			btn.setText("Korean");
			break;
		case INDICATOR_BOTH:
			btn.setText("Both");
			break;
		default:
			break;
		}

		return indicator;
	}
}
